package com.mjk.common.core.drpc;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Objects;

/**
 * rpc dubbo 配置
* @author mjk  
* @date 2019年2月15日
 * 配置中心改为nacos 开关变成nacos的注册中心地址是否打开
 */
@Slf4j
public class DrpcAnnotationBeanFactory {

	@Bean
	public RegistryConfig createDubboRegistry(ConfigurableEnvironment env) {
		RegistryConfig rc = new RegistryConfig();
		rc.setTimeout(Integer.parseInt(env.getProperty(DrpcConstans.DRPC_TIMEOUT, "30000")));
		rc.setAddress(env.getProperty(DrpcConstans.DRPC_DUBBO_REGISTRY_ADDRESS));
		rc.setFile(env.getProperty("user.dir") + "/.dubbo/dubbo-registry.cache");
		log.debug("create register config , address is {}", rc.getAddress());
		return rc;
	}

	@Bean
	public ProtocolConfig protocolConfig(ConfigurableEnvironment env) {
		ProtocolConfig protocol = new ProtocolConfig();
		if (env.containsProperty(DrpcConstans.DRPC_PORT)) {
			protocol.setPort(Integer.parseInt(Objects.requireNonNull(env.getProperty(DrpcConstans.DRPC_PORT))));
		}
		if (env.containsProperty(DrpcConstans.DRPC_DUBBO_THREADS)) {
			protocol.setThreads(Integer.parseInt(Objects.requireNonNull(env.getProperty(DrpcConstans.DRPC_DUBBO_THREADS))));
		}
		//设置传输大小
		if (env.containsProperty(DrpcConstans.DRPC_DUBBO_PAYLOAD)) {
			protocol.setPayload(Integer.parseInt(Objects.requireNonNull(env.getProperty(DrpcConstans.DRPC_DUBBO_PAYLOAD))));
		}
        //协议的消息派发模式
		protocol.setDispatcher("message");
		protocol.setName("dubbo");
		return protocol;
	}
	
	@Bean
	public ProviderConfig createProviderConfig(ConfigurableEnvironment env) {
		ProviderConfig pc = new ProviderConfig();
		if (env.containsProperty(DrpcConstans.DRPC_TIMEOUT)) {
			pc.setTimeout(Integer.parseInt(Objects.requireNonNull(env.getProperty(DrpcConstans.DRPC_TIMEOUT))));
		}
		if (env.containsProperty(DrpcConstans.DRPC_DUBBO_RETRIES)) {
			pc.setRetries(Integer.parseInt(Objects.requireNonNull(env.getProperty(DrpcConstans.DRPC_DUBBO_RETRIES))));
		}
		if (env.containsProperty(DrpcConstans.DRPC_DUBBO_DELAY)) {
			pc.setDelay(Integer.parseInt(Objects.requireNonNull(env.getProperty(DrpcConstans.DRPC_DUBBO_DELAY))));
		}
		if(env.containsProperty(DrpcConstans.DRPC_DUBBO_FILTER)){
			String filters = env.getProperty(DrpcConstans.DRPC_DUBBO_FILTER);
			if(null != filters) {
				if (filters.contains(",")) {
					for (String filter : filters.split(",")) {
						pc.setFilter(filter);
					}
				} else {
					pc.setFilter(filters);
				}
			}
		}
		pc.setFilter("-exception");
		return pc;
	}

	@Bean
	public ConsumerConfig createConsumerConfig(ConfigurableEnvironment env) {
		ConsumerConfig cc = new ConsumerConfig();
		if (env.containsProperty(DrpcConstans.DRPC_DUBBO_CONSUMER_CHECK)) {
			Boolean check = Boolean.parseBoolean(env.getProperty(DrpcConstans.DRPC_DUBBO_CONSUMER_CHECK));
			cc.setCheck(check);
		}
		if (env.containsProperty(DrpcConstans.DRPC_TIMEOUT)) {
			cc.setTimeout(Integer.parseInt(Objects.requireNonNull(env.getProperty(DrpcConstans.DRPC_TIMEOUT))));
		}
		if (env.containsProperty(DrpcConstans.DRPC_DUBBO_RETRIES)) {
			cc.setRetries(Integer.parseInt(Objects.requireNonNull(env.getProperty(DrpcConstans.DRPC_DUBBO_RETRIES))));
		}
		if(env.containsProperty(DrpcConstans.DRPC_DUBBO_FILTER)){
			String filters = env.getProperty(DrpcConstans.DRPC_DUBBO_FILTER);
			if(null != filters) {
				if (filters.contains(",")) {
					for (String filter : filters.split(",")) {
						cc.setFilter(filter);
					}
				} else {
					cc.setFilter(filters);
				}
			}
		}
		cc.setLazy(true);
		cc.setFilter("-exception");		
		return cc;
	}
	 @Bean
    ServiceParameterBeanPostProcessor serviceParameterBeanPostProcessor() {
        return new ServiceParameterBeanPostProcessor();
    }	
}
